<?php

namespace App\Models\Entity\Asset;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Vseriali
 * @package App\Models\Entity\Asset\Video
 * @version August 1, 2017, 4:32 pm UTC
 */
class Iaseriali extends Model
{
    use SoftDeletes;

    public $table = 'iaserialis';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        'iaseriali_no',
        'name',
        'title',
        'subtitle',
        'episode',
        'score',
        'firstdate',
        'iaasset_id',
        'short_desc',
        'long_desc',
        'created_at',
        'updated_at',
        'listorder',

        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',

        // 注入
        'msg_id',
        'sync_result',
        'sync_content',
        'sync_description',
        'async_result',
        'async_content',
        'async_description',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'iaseriali_no' => 'string',
        'name' => 'string',
        'title' => 'string',
        'subtitle' => 'string',
        'episode' => 'integer',
        'iaasset_id' => 'integer',
        'short_desc' => 'string',
        'long_desc' => 'string',
        'listorder'         => 'integer',

        // 注入
        'msg_id'            => 'string',
        'sync_result'       => 'tinyInteger',
        'sync_content'      => 'string',
        'sync_description'  => 'string',
        'async_result'      => 'tinyInteger',
        'async_content'     => 'string',
        'async_description' => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'iaseriali_no' => 'unique:vserialis,iaseriali_no',
        'name' => 'required|max:100',
        'title' => 'max:100',
        'subtitle' => 'max:100',
        'episode' => 'required|numeric',
        'score' => 'numeric',
        'firstdate' => 'date',
        'online_at' => 'date',
        'offline_at' => 'date',
        'short_desc' => 'required|max:100'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 剧集
     **/
    public function iaasset()
    {
        return $this->hasOne(\App\Models\Entity\Asset\Iaasset::class, 'id', 'iaasset_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 文件
     **/
    public function vifiles()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Media\Vifile::class, 'iaseriali_vifiles', 'iaseriali_id', 'vifile_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     * 图片
     **/
    public function vposteris()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Media\Vposteri::class, 'iaseriali_vposteris', 'iaseriali_id', 'vposteri_id');
    }

    /**
    * 过滤上下线数据
    **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
    * 过滤上线发布数据
    **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
    * 过滤下线发布数据
    **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }

    /**
     * 获得此媒资的所有标签。
     */
    public function tags()
    {
        return $this->morphToMany(\App\Models\Entity\tag_content\tag_content::class, 'tag_content');
    }



}
